package com.example.demo.threadlocal;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * https://blog.csdn.net/whut2010hj/article/details/81413887
 */
public class ThreadLocalTest {

    public static void main(String[] args) throws InterruptedException {

        class RunApple implements Runnable {

            private final int id;

            RunApple(int id) {
                this.id = id;
            }


            @Override
            public void run() {
                while (!Thread.currentThread().isInterrupted()) {
                    ThreadLocalCache.set(String.valueOf(id), new Apple(id));
                    Thread.yield();
                    System.out.println(this);
                }
            }

            @Override
            public String toString() {
                return "#" + id + ":" + ThreadLocalCache.getCache(String.valueOf(id));
            }
        }

        ExecutorService exec = Executors.newCachedThreadPool();
        for (int i = 0; i < 5; i++) {
            exec.execute(new RunApple(i));
        }
        TimeUnit.SECONDS.sleep(1);

        exec.shutdownNow();

    }
}
